package com.shuqi.activity.viewport;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuqi.controller.R;

public class CommonTitle extends LinearLayout {

    private View view;
    private TextView titleTextView;
    public ImageView leftImageView;
    private ImageView rightImageView;

    public CommonTitle(Context context) {
        super(context);
    }

    public CommonTitle(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.common_title, this);
        titleTextView = (TextView) view.findViewById(R.id.title_text);
        leftImageView = (ImageView) view.findViewById(R.id.title_left);
        rightImageView = (ImageView) view.findViewById(R.id.title_right);

        TypedArray typeArray = context.obtainStyledAttributes(attributeSet, R.styleable.title);
        String strTitle = typeArray.getString(R.styleable.title_titleText);
        Drawable imgLeft = typeArray.getDrawable(R.styleable.title_leftbg);
        Drawable imgRight = typeArray.getDrawable(R.styleable.title_rightbg);
        float imgLeftDefWidth = context.getResources().getDimension(R.dimen.common_title_left_img_width);
        float imgRightDefWidth = context.getResources().getDimension(R.dimen.common_title_right_img_width);
        float imgLeftWidth = typeArray.getDimension(R.styleable.title_left_width, imgLeftDefWidth);
        float imgRightWidth = typeArray.getDimension(R.styleable.title_right_width, imgRightDefWidth);

        titleTextView.setText(strTitle);
        leftImageView.setImageDrawable(imgLeft);
        rightImageView.setImageDrawable(imgRight);

        ViewGroup.LayoutParams leftLp = leftImageView.getLayoutParams();
        ViewGroup.LayoutParams rightLp = rightImageView.getLayoutParams();
        leftLp.width = (int)imgLeftWidth;
        rightLp.width = (int)imgRightWidth;
        
        leftImageView.setLayoutParams(leftLp);
        rightImageView.setLayoutParams(rightLp);
    }

    public void setTitle(String strTitle) {
        titleTextView.setText(strTitle);
    }

    public void setLeftImage(int imageId) {
        leftImageView.setImageDrawable(getResources().getDrawable(imageId));
    }

    public void setRightImage(int imageId) {
        rightImageView.setImageDrawable(getResources().getDrawable(imageId));
    }

    public void setStyle(boolean leftVisible, boolean rightVisible) {
        leftImageView.setVisibility(leftVisible ? View.VISIBLE : View.INVISIBLE);
        rightImageView.setVisibility(rightVisible ? View.VISIBLE : View.INVISIBLE);
    }

    public void setImageListener(OnClickListener imageListener) {
        if (imageListener != null) {
            leftImageView.setOnClickListener(imageListener);
            rightImageView.setOnClickListener(imageListener);
        }
    }

    public void setLeftClickListener(OnClickListener leftListener) {
        if (leftListener != null) {
            leftImageView.setOnClickListener(leftListener);
        }
    }

    public void setRightClickListener(OnClickListener rightListener) {
        if (rightListener != null) {
            rightImageView.setOnClickListener(rightListener);
        }
    }
}
